﻿Public Partial Class Slop_Popup
    Inherits System.Web.UI.Page
    Dim clsGet As New ClassFunctionGetData
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Not IsPostBack Then
            If (Not (Session("Slop") Is Nothing)) Then
                Dim dtSLop As DataTable = clsGet.Get_Slop(Session("Slop"), "")
                ViewState("Slop") = Session("Slop")
                If (dtSLop.Rows.Count > 0) Then
                    txtSlop.Text = dtSLop.Rows(0).Item("Slop_Name")
                    txtDescription.Text = dtSLop.Rows(0).Item("Slop_Description")
                End If
            Else
                ViewState("Slop") = ""
            End If
            If (Session("EditMaster") Is Nothing) Then
                btnSave.Visible = False
            End If
        End If
    End Sub

    Private Sub btnCancel_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnCancel.Click
        Session("Slop") = Nothing
        ScriptManager.RegisterClientScriptBlock(Me.Page, GetType(String), "myscript", " parent.closeModal();", True)
    End Sub

    Private Sub btnSave_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnSave.Click
        If (txtSlop.Text.Trim() = "") Then
            ScriptManager.RegisterClientScriptBlock(Me.Page, GetType(String), "myscript", " alert('กรุณาทำการระบุ Slop Name');", True)
            Exit Sub
        End If
        Dim conn As New System.Data.SqlClient.SqlConnection(connString)
        Dim clsDB As New ClassDatabase
        Dim clsFc As New ClassFunction
        Dim condition As Boolean = True
        Dim tran As System.Data.SqlClient.SqlTransaction = Nothing
        Application.Lock()
        conn.Open()
        Dim Line_Code As String = ""
        If ViewState("Slop").ToString() = "" Then
            Line_Code = clsDB.GeneratorID(conn, "Slop_Master")
        Else
            Line_Code = ViewState("Slop").ToString()
        End If

        '  tran = clsDB.BeginTran(conn)
        If (clsFc.InsertSlop(ViewState("Slop").ToString(), Line_Code, txtSlop.Text, txtDescription.Text, "0", conn, tran) > 0) Then
            Session("Slop") = Nothing
            ScriptManager.RegisterClientScriptBlock(Me.Page, GetType(String), "myscript", "parent.closeModal();alert('บันทึกข้อมูลเสร็จสมบูรณ์ !!');", True)
        Else
            ScriptManager.RegisterClientScriptBlock(Me.Page, GetType(String), "myscript", "alert('เกิดความผิดพลาดในการบันทึกข้อมูล !!');", True)
        End If
        conn.Close()
        Application.UnLock()
        conn.Dispose()
    End Sub

    Private Sub MainPage_PreInit(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.PreInit
        Dim ds As DataSet = Session("UserData")
        If Not (ds Is Nothing) Then
            Page.MasterPageFile = clsGet.GetMaster_Page(ds.Tables(0).Rows(0).Item("User_Name").ToString(), "")
        End If
    End Sub

End Class